package cn.stylefeng.guns.modular.ry600.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import cn.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;

import cn.stylefeng.guns.modular.system.model.Order;
import cn.stylefeng.guns.modular.ry600.service.IOrderService;

/**
 * 连锁订单主表控制器
 *
 * @author fengshuonan
 * @Date 2018-12-14 11:34:59
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/ry600/order/";

    @Autowired
    private IOrderService orderService;
    

    /**
     * 跳转到连锁订单主表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "order.html";
    }

    /**
     * 跳转到添加连锁订单主表
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "order_add.html";
    }

    /**
     * 跳转到修改连锁订单主表
     */
    @RequestMapping("/order_update/{orderId}")
    public String orderUpdate(@PathVariable Integer orderId, Model model) {
        Order order = orderService.selectById(orderId);
        model.addAttribute("item",order);
        LogObjectHolder.me().set(order);
        return PREFIX + "order_edit.html";
    }

    /**
     * 获取连锁订单主表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return orderService.selectList(null);
    }

    /**
     * 新增连锁订单主表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        orderService.insert(order);
        return SUCCESS_TIP;
    }

    /**
     * 删除连锁订单主表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer orderId) {
        orderService.deleteById(orderId);
        return SUCCESS_TIP;
    }

    /**
     * 修改连锁订单主表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Order order) {
        orderService.updateById(order);
        return SUCCESS_TIP;
    }

    /**
     * 连锁订单主表详情
     */
    @RequestMapping(value = "/detail/{orderId}")
    @ResponseBody
    public Object detail(@PathVariable("orderId") Integer orderId) {
        return orderService.selectById(orderId);
    }
    
    @RequestMapping(value = "/selectByMsg")
    @ResponseBody
    public Object selectByMsg(HttpServletRequest request, HttpServletResponse response) {
    	Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
    	Map<String, Object> msg=this.setMap(request);
    	PageHelper.startPage(page, rows);
    	List<Order> result=(List<Order>) orderService.selectByMsg(msg);
    	
    	return result;
        
    }
    
    public Map<String,Object> setMap(HttpServletRequest request){
    	Map<String, Object> msg = new HashMap<String, Object>();
    	String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		String searchKey1 = request.getParameter("searchKey1");
		String searchWord1 = request.getParameter("searchWord1");
		
		String neBind = request.getParameter("neBind");
		
		if (!StringUtils.isBlank(neBind) ) {
			if("0".equals(neBind)){
				msg.put("neBind", neBind);
			}else{
				msg.put("yeBind", neBind);
			}
		}
		
		if (!StringUtils.isBlank(searchKey) && !StringUtils.isBlank(searchWord)) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		if (!StringUtils.isBlank(searchKey1) && !StringUtils.isBlank(searchWord1)) {
			msg.put(searchKey1.trim(), searchWord1.trim());
		}
		return msg;
    }
    
}
